package cn.sccl.dirms.topo.discovery.controller;

import cn.sccl.common.model.PageResult;
import cn.sccl.common.web.BaseController;
import cn.sccl.dirms.topo.discovery.model.DiscoverReTaskRule;
import cn.sccl.dirms.topo.discovery.model.DiscoverRuleExtends;
import cn.sccl.dirms.topo.discovery.service.DiscoverReTaskRuleManager;
import cn.sccl.dirms.topo.discovery.service.DiscoverRuleManager;
import cn.sccl.dirms.topo.discovery.service.DiscoverTaskManager;
import com.fasterxml.jackson.annotation.JsonView;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* 自动发现任务与规则的关系表 的Controller
* Created by _minus on 2017-09-25.
*/
@RestController
@RequestMapping(value = "/nms/topo/discovery/discoverReTaskRule")
public class DiscoverReTaskRuleController extends BaseController {

    @Autowired
    private DiscoverReTaskRuleManager discoverReTaskRuleManager;

    @Autowired
    private DiscoverRuleManager discoverRuleManager ;

    @Autowired
    private DiscoverTaskManager discoverTaskManager ;



    /**
    * 显示所有数据
    */
    @RequestMapping(value = "/all" , method = RequestMethod.GET)
    public List<DiscoverReTaskRule> listAllDiscoverReTaskRule(){
        return discoverReTaskRuleManager.listAll();
    }

    /**
    * 分页查询，分页参数放置在url路径中
    * @param pageIndex 页码
    * @param pageSize 分页大小
    * @param request
    * @return
    */
    @RequestMapping(value = "/page/{pageIndex}/{pageSize}" , method = RequestMethod.GET)
    public PageResult<DiscoverReTaskRule> pageQueryDiscoverReTaskRule(@PathVariable("pageIndex") long pageIndex , @PathVariable("pageSize") int pageSize , HttpServletRequest request){
        return discoverReTaskRuleManager.pageQueryByLike(pageIndex, pageSize, getQueryData(request));
    }

    /**
    * 分页查询，分页参数放置在request中
    * @param request
    * @return
    */
    @RequestMapping(value = "/page" , method = RequestMethod.GET )
    public PageResult<DiscoverReTaskRule> pageQueryDiscoverReTaskRule(HttpServletRequest request){
        return discoverReTaskRuleManager.pageQueryByLike(getPageIndex(request), getPageSize(request), getQueryData(request));
    }

    /**
     *
     * @param discoverReTaskRules
     * @return
     */
    @RequestMapping(value = "/save/list" , method = RequestMethod.POST)
    public List<DiscoverReTaskRule> saveDiscoverReTaskRules(@RequestBody List<DiscoverReTaskRule> discoverReTaskRules){
        if (!discoverReTaskRules.isEmpty()){
            discoverReTaskRuleManager.saveBatch(discoverReTaskRules);
            //需要 更改下t_topo_discover_task 表中的rule_detail 字段的值 ；
            Set<String> taskIdSet =new HashSet();
            for (DiscoverReTaskRule discoverReTaskRule : discoverReTaskRules) {
                taskIdSet.add(discoverReTaskRule.getTaskId());
            }
            for (String taskId : taskIdSet) {
                discoverTaskManager.updateDiscoverRuleDetail(taskId);
            }
        }
        return discoverReTaskRules ;
    }

    /**
    * 保存数据
    * @param discoverReTaskRule
    * @return
    */
    @RequestMapping(value = "/" , method = RequestMethod.POST)
    public DiscoverRuleExtends saveDiscoverReTaskRule(@RequestBody DiscoverReTaskRule discoverReTaskRule){
        //需要判断下 task id 与rule id 关系的表中 应该是不可重复的，避免同一规则,重复发现topo图,

        discoverReTaskRuleManager.save(discoverReTaskRule);
        //需要 更改下t_topo_discover_task 表中的rule_detail 字段的值 ；
        if (!StringUtils.isEmpty(discoverReTaskRule.getTaskId())){
            discoverTaskManager.updateDiscoverRuleDetail(discoverReTaskRule.getTaskId());
        }
        return discoverRuleManager.findDiscoverRuleExtendsByRelationTaskRule(discoverReTaskRule);
    }

    /**
    * 根据ID删除数据
    * @param id
    */
    @RequestMapping(value = "/{id}" , method = RequestMethod.DELETE)
    @JsonView(Object.class)
    public void deleteDiscoverReTaskRule(@PathVariable("id") String id){
        DiscoverReTaskRule taskRule = discoverReTaskRuleManager.get(id);
        if (taskRule != null) {
            discoverReTaskRuleManager.delete(id);
            discoverTaskManager.updateDiscoverRuleDetail(taskRule.getTaskId());

        }


    }
}
